
/*
 *  
 *
 *  Copyright (C) Microsoft Corporation.  All rights reserved
 *
 *  
 */

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CRMScenarios
{
	public partial class PickListForm : Form
	{
        private InsertFlds parentController = null;
        private string _thisPickListEntity = null;
        StringCollection items = null;
        bool ChangedProgrammatically = false;

		public PickListForm()
		{
			InitializeComponent();
            checkedListBox1.MouseDoubleClick += new MouseEventHandler(checkedListBox1_SelectedIndexChanged);
		}

        public void InitPickListForm(string entityType, InsertFlds parent, StringCollection defaultAtrribs)
        {
            if ((parent == null) || (entityType == null))
            {
                return;
            }

            parentController = parent;
            _thisPickListEntity = entityType;
            this.checkedListBox1.Items.Clear();

            // TRY CATCH
            string[][] formXmlForThisEntity = CrmConnector.getFormViewDescription(entityType);
            string[] attrsToBeRetrieved = CrmConnector.GetAttributeNamesFromFormXmlIF(formXmlForThisEntity);
            string[] attributesAsToDisplay = CrmConnector.GetDisplayNameOfAttributes(entityType, attrsToBeRetrieved);
            if ((formXmlForThisEntity == null) || (attributesAsToDisplay == null) || (attributesAsToDisplay.Length == 0))
            {
                lblError.Visible = true;
                cRoxSelectAll.Enabled = false;
                btnOk.Enabled = false;
                return;
            }
            string[] checkedItemArr = CrmConnector.GetDisplayNameOfAttributes(entityType, CrmConnector.CollectionToArray(defaultAtrribs));
            StringCollection checkedItemColl = CrmConnector.ArrayToCollection(checkedItemArr);
            items = new StringCollection();

            foreach (string s in attributesAsToDisplay)
            {
                if ((s == null) || this.checkedListBox1.Items.Contains((object)s) || (s == string.Empty) )
                {
                    continue;
                }
                items.Add(s);
                this.checkedListBox1.Items.Add((object)s, (( checkedItemColl.Contains(s))? (CheckState.Checked) : (CheckState.Unchecked)));
            }

            this.checkedListBox1.SelectedIndexChanged += new EventHandler(checkedListBox1_SelectedIndexChanged);
        }

        void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((checkedListBox1.CheckedItems.Count == checkedListBox1.Items.Count) &&
                 (cRoxSelectAll.Checked == false))
            {
                ChangedProgrammatically = true;
                cRoxSelectAll.Checked = true;
                return;
            }

            if (cRoxSelectAll.Checked == true)
            {
                ChangedProgrammatically = true;
                cRoxSelectAll.Checked = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (parentController == null)
            {
                this.Dispose();
                return;
            }
            string[] strArrSelected = new string[checkedListBox1.CheckedItems.Count];
            int end = 0;
            foreach (object item in checkedListBox1.CheckedItems)
            {
                strArrSelected[end++] = (string)item;
            }

            if (strArrSelected.Length == 0)
            {
                Util.ShowMsg(global::CRMScenarios.Resources.NoColumnsSelected);
                return;
            }

            parentController.InsertRelatedRecordsWithAdditionalColumns(strArrSelected);
            this.Dispose();
        }

        private void cRoxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ChangedProgrammatically)
            {
                ChangedProgrammatically = false;
                return;
            }
            checkedListBox1.Items.Clear();
            foreach (string s in items)
            {
                checkedListBox1.Items.Add(((object)s), cRoxSelectAll.Checked);
            }
        }
    }
}